layui.config({
    base: '../portal/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use('index');

var element, layer;
$(function () {
    layui.use(['element', 'layer'], function () {
        element = layui.element;
        layer = layui.layer;
        loadData();
    });

    var token = getUrlParam("token");
    if (token) {
        document.cookie = "token=" + token;
        var currentUrl = window.location.href;
        window.location = currentUrl.split("?")[0];
    }

    $("#logout").on("click", function () {
        delCookie("token");
        var currentUrl = window.location.href;
        window.location = authApiPrefix + "/login/logout?url=" + currentUrl.split("?")[0];
    });
    $("#menu-more").on('click', function () {
        var ss = $(this).parent().children("dl");
        //ss.addClass("layui-show");
        ss.toggleClass("layui-show");
    });
});

var setCurrentUser = function (userInfo) {
    var name = "";
    if (userInfo["userName"]) {
        name = userInfo['userName'];
    } else if (userInfo["realName"]) {
        name = userInfo['realName'];
    } else {
        name = userInfo['ehrId'];
    }
    document.getElementById("userName").innerText = name;
};

function loadData() {
    $.ajax({
        type: 'get',
        headers: {"token": getCookie("token")},
        url: menuUrl,
        dataType: 'json',
        success: function (result) {
            if (result.code === 0) {
                if (result.data["userInfo"]) {
                    setCurrentUser(result.data["userInfo"]);
                }
                var str = "";
                str += "<dd data-name=\"console\" class=\"layui-nav-child layui-nav-item\">\n" +
                    "                        <a lay-id=\"home\" lay-href=\"home/home.html\">首页</a>\n" +
                    "                    </dd>";
                if (result.data['menu'].length > 0) {
                    for (var i = 0; i < result.data['menu'].length; i++) {
                        var v = result.data.menu[i];
                        var subs = v['subResources'];
                        var subStr = "";
                        if (subs != null && subs.length > 0) {
                            str += "<li data-name=\"" + v['resourceName'] + "\" class=\"layui-nav-item\">";
                            str += "<a href=\"javascript:;\" lay-tips=\"" + v['resourceName'] + "\" lay-direction=\"2\">";
                            str += "<cite>" + v['resourceName'] + "</cite> <span class=\"layui-nav-more\"></span></a>";
                            subStr += "<dl class=\"layui-nav-child\">";
                            for (var j = 0; j < subs.length; j++) {
                                var sub = subs[j];
                                subStr += "<dd data-name=\"console\">";
                                subStr += "<a lay-id=\"" + sub['id'] + "\" lay-href=\"" + sub['resourceUrl'] + "\">" + sub['resourceName'] + "</a>"
                                subStr += "</dd>";
                            }
                            subStr += "</dl>";
                            str += subStr + "</li>";
                        } else {
                            if (v['resourceUrl']) {
                                str += "<dd data-name=\"console\" class=\"layui-nav-child layui-nav-item\">\n" +
                                    "                        <a lay-id=\"" + v['id'] + "\" lay-href=\"" + v['resourceUrl'] + "\">" + v['resourceName'] + "</a>\n" +
                                    "                    </dd>";
                            } else {
                                str += "<a href=\"javascript:;\" lay-tips=\"" + v['resourceName'] + "\" lay-direction=\"2\">";
                                str += "<cite>" + v['resourceName'] + "</cite> <span class=\"layui-nav-more\"></span></a>";
                            }
                        }
                    }
                }

                $('#LAY-system-side-menu').html(str);
                element.render('nav');
            } else if (result.code === 3000) {
                var currentUrl = window.location.href;
                var url = redirectUrl + "?url=" + currentUrl.split("?")[0];
                window.location = url;
            } else {
                layer.msg(result.msg, {
                    time: 1000
                }, function () {

                });
            }
        }
    });
}